package com.zmx.b2c.stock.repository;

import com.zmx.b2c.stock.entity.ProductStock;
import com.zmx.common.common.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author zhaomxr
 */
public interface ProductStockRepository extends BaseRepository<ProductStock> {

    ProductStock findByProductId(Long productId);

    List<ProductStock> findByProductIdIn(Long[] productIds);

    @Modifying
    @Query(value = "update product_stock set order_occupy = order_occupy + ?1, available_stock = available_stock - ?1 where product_id = ?2 and available_stock - ?1 >= 0", nativeQuery = true)
    void updateStock(int quantity, Long productId);
}
